import os
import random
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from api_key.model import ApiKey
from api_key import get_api_key_by_key

def generate_api_key(name, email):
    return 'api_key' + str(name) + str(email) + ('%s' % random.random())

def generate_secret_key(name, email):
    return 'secret_key' + str(name) + str(email) + ('%s' % random.random())

class ListApiKey(webapp.RequestHandler):
    def get(self):
        apis = ApiKey.all().fetch(1000, 0)
        status = self.request.get('status')
        if status == 'edit':
            note = 'Edit successfully !'
        elif status == 'delete':
            note = 'Delete successfully !'
        else:
            note = ''
        
        template_values = {
            'apis' : apis,
            'status': note
        }

        path = os.path.join(os.path.dirname(__file__), 'list.html')
        self.response.out.write(template.render(path, template_values))

        
class EditApiKey(webapp.RequestHandler):
    def get(self):
        key =  self.request.get('key')
        api = get_api_key_by_key(key)
        
        template_values = {
            'api' : api
        }
        
        path = os.path.join(os.path.dirname(__file__), 'edit.html')
        self.response.out.write(template.render(path, template_values))
        
    def post(self):
        #self.response.out.write('EditApiKey post')
        name = self.request.get('api_name').decode('utf-8')
        email = self.request.get('api_email').decode('utf-8')
        key = self.request.get('api_key').decode('utf-8')
        enabled = True if self.request.get('api_enabled') == 'on' else False
        
        api = get_api_key_by_key(key)
        api.name = name
        api.email = email
        api.enabled = enabled
        api.put()
        self.redirect('/api_key/list?status=edit', True)

class DeleteApiKey(webapp.RequestHandler):
    def get(self):
        key = self.request.get('key')
        api = get_api_key_by_key(key)
        api.delete()
        self.redirect('/api_key/list?status=delete', True)
    def post(self):
        self.response.out.write('DeleteApiKey post asf asfd asdf adfs aswqe qwrwq')


class AddApiKey(webapp.RequestHandler):
    def get(self):
        key = self.request.get('added')
        # TODO api = find api_key where __key__ = key
        api = get_api_key_by_key(key)

        template_values = {
            'api' : api
        }

        path = os.path.join(os.path.dirname(__file__), 'add.html')
        self.response.out.write(template.render(path, template_values))

    def post(self):
        api = ApiKey()
        api.name = self.request.get('api_name').decode('utf-8')
        api.email = self.request.get('api_email').decode('utf-8')
        api.enabled = True if self.request.get('api_enabled') == 'on' else False
        api.api_key = generate_api_key(api.name, api.email)
        api.secret_key = generate_secret_key(api.name, api.email)
        api.put()
        self.redirect('/api_key/add?added=%s' % api.key(), True)